package com.kfd.controller;

import com.kfd.bean.Message;
import com.kfd.mvc.ResponseBody;
import com.kfd.service.EadminService;
import com.kfd.util.JSONUtil;
import com.kfd.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class AdminController {

    @ResponseBody("/admin/login.do")
    public String adminLogin(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String login = EadminService.login(username, password);
        Message message;
        if(login != null){
            message = new Message(0,"登陆成功");
            //更新登录时间和ip
            Date date = new Date();
            String ip = request.getRemoteAddr();
            EadminService.updateLoginIpTime(username,date,ip);
            request.getSession().setAttribute("adminUserName",username);
            request.getSession().setAttribute("adminUserPhone",login);

        }else{
            message = new Message(-1,"登陆失败，用户名或密码错误");
        }
        return JSONUtil.toJSON(message);
    }
}
